package com.example.yangyinzhaixian;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.yangyinzhaixian.Bean.MyUpload_bean;
import com.example.yangyinzhaixian.uitl.SharedPreferencesUtils;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

public class Upload2Activity extends AppCompatActivity implements View.OnClickListener {
    private int i=1;
    private Button Upload_button;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == i) {
                if (frist.equals("frist"))
                {
                    Intent intent1 = new Intent(Upload2Activity.this, CourseActivity.class);
                    startActivity(intent1);
                    finish();
                }
                else
                {
                    Intent intent = new Intent(Upload2Activity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }

            }
        }
    };
    private RelativeLayout activity_upload2;
    private ImageView Up_image;
    private String frist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_upload2);
        initView();
        frist = (String) SharedPreferencesUtils.getParam(this, "frist", "");
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        StringRequest stringRequest = new StringRequest("http://api.immedc.com/restapi/loading/getStartBanner", new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {

                        Gson gson = new Gson();
                        MyUpload_bean myBaseApplication = gson.fromJson(s, MyUpload_bean.class);
                        String bannerUrl = myBaseApplication.getData().getBannerUrl();
                Picasso.with(Upload2Activity.this).load(bannerUrl).into(Up_image);
//                        ImageCache.getInstance(Upload2Activity.this).displayImage(Up_image,bannerUrl,-1);
                        handler.sendEmptyMessageDelayed(1, 2000);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        requestQueue.add(stringRequest);
    }

    private void initView() {
        Upload_button = (Button) findViewById(R.id.Upload_button);
        Upload_button.setOnClickListener(this);
        activity_upload2 = (RelativeLayout) findViewById(R.id.activity_upload2);
        Up_image = (ImageView) findViewById(R.id.Up_image);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Upload_button:
                if (frist.equals("frist"))
                {
                    Intent intent1 = new Intent(Upload2Activity.this, CourseActivity.class);
                    startActivity(intent1);
                    i=2;
                    finish();
                }
                else
                {
                    Intent intent = new Intent(Upload2Activity.this, MainActivity.class);
                    startActivity(intent);
                    i=2;
                    finish();
                }
                break;
        }
    }
}
